﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Century.Web.Security.Attributes
using GKFX.Common.Business.BOL.Common;
using Telerik.Web.Mvc;
using Century.Model;
using GKFX.Common.UI.MVC;
namespace Century.Web.MVC.AdminArea
{
    [Authorization(Group = "", Name = "Administration")]
    public class RolePermissionManagementController : CenturyAdminBaseController
    {
        //
        // GET: /RolePermissionManagement/

        RoleBSO roleBSO = new RoleBSO();
        PermissionItemBSO permissionItemBSO = new PermissionItemBSO();

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetRoleList()
        {
            var model = new GridModel
            {
                Data = roleBSO.GetAll().Select(e => new { e.Id, e.RoleName, e.DisplayName })
            };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult EditRole(int id)
        {
            GKFX.Models.Common.Role role = roleBSO.Find(c => c.Id == id);
            TryUpdateModel(role);
            roleBSO.Update(role);
            var model = new GridModel
            {
                Data = roleBSO.GetAll().Select(e => new { e.Id, e.RoleName, e.DisplayName })
            };
            return View(model);
        }

        [GridAction]
        public ActionResult InsertRole()
        {
            Role role = new Role();
            if (TryUpdateModel(role))
            {
                roleBSO.Insert(role);
            }
            var d = new GridModel
            {
                Data = roleBSO.GetAll().Select(e => new { e.Id, e.RoleName, e.DisplayName })
            };
            return View(d);
        }

        [GridAction]
        public ActionResult GetPermissionsForRole(int roleId)
        {
            IEnumerable<GKFX.Models.Common.PermissionItem> permissions = permissionItemBSO.GetAll();
            IEnumerable<GKFX.Models.Common.PermissionItem> rolePermissions = roleBSO.GetPermissionsForRole(roleId);
            var confDetails = permissions.Select(c =>
                new
                {
                    c.Id,
                    c.Name,
                    c.DisplayName,
                    c.RequiredAdministrator,
                    RoleHasThisPermission = rolePermissions.Any(a => a.Name == c.Name)
                });
            return View(new GridModel(confDetails));
        }

        public string ChangeRolePermission(int roleId, int permissionId, bool roleHasThisPermission)
        {
            GKFX.Models.Common.Role role = roleBSO.Find(c => c.Id == roleId, "PermissionItems");
            GKFX.Models.Common.PermissionItem permission = roleBSO.FindPermissionItem(permissionId);
            if (roleHasThisPermission)
            {
                role.PermissionItems.Add(permission);
            }
            else
            {
                role.PermissionItems.Remove(permission);
            }
            roleBSO.Update(role);
            return string.Empty;
        }

    }
}
